package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.dao.TagDAO;
import com.junzhiit.nengji.shop.model.Tag;
import com.junzhiit.nengji.shop.model.enums.TagType;

@Repository
public class TagDAOImpl extends AbstractBaseDAO<Tag> implements TagDAO{

	@Override
	public List<Tag> findAll(Tag tag, int pageNumber, int maxPageRow){
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from tag ");
		sql.append(" where is_delete=? ");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		return super.queryForList(sql.toString(), Tag.class, param);
	}
	
	@Override
	public Tag findById(String id){
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from tag");
		sql.append(" where is_delete = ? ");
		sql.append(" and id=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(id);
		
		return super.query(sql.toString(), Tag.class,param);
	}
	
	@Override
	public int getTotalCount(Tag tag){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from tag");
		sql.append(" where is_delete=? ");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		return super.queryForInt(sql.toString(), param);
	}
	
	@Override
	public List<Tag> getAll(Tag tag){
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from tag");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(tag.getType()!=null){
			sql.append(" and type=?");
			param.setInt(tag.getType());
		}
		
		
		return super.queryForList(sql.toString(),Tag.class,param);
	}

	@Override
	public List<Tag> getAllByGoodsId(String goodsId) {
		StringBuffer sql=new StringBuffer();
		StatementParameter param=new StatementParameter();
		
		sql.append("select * from tag t where find_in_set( t.id,( ");
		sql.append(" select ifnull(group_concat(tag_id),'') from goods_tag  gt where gt.goods_id = ? ");
		sql.append(" ))>0 and t.type = ? ");
		
		param.setString(goodsId);
		param.setInt(TagType.GOODS.toIntValue());
		
		return super.queryForList(sql.toString(),Tag.class,param);
	}
}
